package com.woniuxy.microsysserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.MemberEntity;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microsysserver.entity.CouponEntity;
import com.woniuxy.microsysserver.entity.MemberCouponEntity;
import com.woniuxy.microsysserver.entity.MyPage;
import com.woniuxy.microsysserver.service.CouponService;
import com.woniuxy.microsysserver.service.MemberCouponService;
import com.woniuxy.microsysserver.uitl.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Auther: wuson  <br/>
 * Date: 2021/4/15:20:04  <br/>
 * Description:
 */
@RefreshScope
@Api("优惠券控制")
@Slf4j
@RequestMapping("/sys/membercoupon")
@RestController
public class MemberCouponController {
    @Autowired(required = false)
    private MemberCouponService memberCouponService;

    @ApiOperation("领取任意优惠券,couponIds以字符串','分隔")
    @PostMapping(value = "/getcoupon", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean getcoupon (@RequestBody MemberEntity memberEntity) {
        //解析前端优惠券id字符串（，分割）
        List<Integer> currentCouponIds = new ArrayList<>();
        String[] split = memberEntity.getCouponIds().split(",");
        for (String s : split) {
            try {
                //前端上传数据格式异常
                currentCouponIds.add(Integer.valueOf(s));
            } catch (Exception e) {
                return ResultBean.builder().code(608).data(Constant.WRONG).build();
            }
        }

        int state = memberCouponService.getcoupon(memberEntity.getUsername(), currentCouponIds);
        return ResultBean.builder().code(200).data(Constant.SUCCESSFUL).build();
    }

    @ApiOperation("查询个人优惠券")
    @PostMapping(value = "/selectCouponByUsername", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean getcouponByUsername (@RequestBody MemberEntity memberEntity) {
        List<MemberCouponEntity> memberCouponEntities = memberCouponService.getcouponByUsername(memberEntity.getUsername());
        return ResultBean.builder().code(200).msg(Constant.SUCCESSFUL).data(memberCouponEntities).build();
    }

    @ApiOperation("查询个人已使用优惠券")
    @PostMapping(value = "/selectUsedCouponByUsername", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean selectUsedCouponByUsername (@RequestBody MemberEntity memberEntity) {
        List<MemberCouponEntity> memberCouponEntities = memberCouponService.selectUsedCouponByUsername(memberEntity.getUsername());
        return ResultBean.builder().code(200).msg(Constant.SUCCESSFUL).data(memberCouponEntities).build();
    }


}
